package com.hucc.api.config;

import com.hucc.api.util.HttpUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * http://127.0.1:8080/swagger-ui/index.html#/
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig implements CommandLineRunner {
	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.paths(PathSelectors.any())
				.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("api管理平台")
				.description("api管理平台描述")
				.version("1.0")
				.build();
	}

	@Override
	public void run(String... args) throws Exception {
        System.out.println("swagger启动成功");
		String json = HttpUtil.get("http://127.0.0.1:8080/v2/api-docs");

		String post = HttpUtil.post("http://127.0.0.1:8080/openapi/collectSwaggerDoc", json);
		System.out.println(post);
	}
}
